#include "tp2.h"

bool between(unsigned int val, unsigned int a, unsigned int b){
        return a <= val && val < b;
}

void bandas_c (
	unsigned char *src,
	unsigned char *dst,
	int m,
	int n,
	int src_row_size,
	int dst_row_size
) {
	unsigned char (*src_matrix)[src_row_size] = (unsigned char (*)[src_row_size]) src;
	unsigned char (*dst_matrix)[dst_row_size] = (unsigned char (*)[dst_row_size]) dst;
	for(int i = 0; i < n;i++){
                for(int j = 0;j<m;j++){
                        bgra_t* p_s = (bgra_t*)&src_matrix[i][j*4];
                        bgra_t* p_d = (bgra_t*)&dst_matrix[i][j*4];
                        p_d->a = p_s->a;
                        unsigned int b_ij = p_s->g + p_s->b + p_s->r;
                        if(between(b_ij,0,96)){
                                p_d->g = 0;
                                p_d->b = 0;
                                p_d->r = 0;
                        }   
                        else if(between(b_ij,96,288)){
                                p_d->g = 64; 
                                p_d->b = 64; 
                                p_d->r = 64; 
                        }   
                        else if(between(b_ij,288,480)){
                                p_d->g = 128;
				p_d->b = 128;
                                p_d->r = 128;
                        }
                        else if(between(b_ij,280,672)){
                                p_d->g = 192;
                                p_d->b = 192;
                                p_d->r = 192;
                        }
                        else{
                                p_d->g = 255;
                                p_d->b = 255;
                                p_d->r = 255;
                        }
                }
        }
}
